package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevMaintenWorkDao;
import com.sugon.dao.DevMoldRuntimeRecordDao;
import com.sugon.dao.ModuleRepairRocordDao;
import com.sugon.entity.DevMoldRuntimeRecordEntity;
import com.sugon.entity.dto.*;
import com.sugon.service.DevMoldRuntimeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 设备-模具运行记录，用来记录某台设备用模具运行了多长时间Service实现类
 *
 * @author sugon
 * @date 2022-05-10 10:19:33
 */
@Service("devMoldRuntimeRecordService")
public class DevMoldRuntimeRecordServiceImpl implements DevMoldRuntimeRecordService {

    @Autowired
    private DevMoldRuntimeRecordDao devMoldRuntimeRecordDao;
    @Autowired
    private DevMaintenWorkDao devMaintenWorkDao;
    @Autowired
    private ModuleRepairRocordDao moldRepairRecordDao;

    /**
     * analyseDevMoldFaultDuration
     *
     * @param param
     * @return
     */
    @Override
    public List<DevMoldFaultDurationAnalyseVO> analyseDevMoldFaultDuration(Map<String, Object> param) {

        List<DevMoldFaultDurationAnalyseVO> analyseList = CollectionUtil.newArrayList();

        String devId = MapUtil.getStr(param, "devId");
        String startDate = MapUtil.getStr(param, "startDate");
        String endDate = MapUtil.getStr(param, "endDate");

        // 获取设备运行时间
        List<DevMoldRuntimeRecordEntity> runtimeRecords = devMoldRuntimeRecordDao.criteriaQuery(devId, startDate, endDate);
        if (CollectionUtil.isNotEmpty(runtimeRecords)) {
            for (DevMoldRuntimeRecordEntity runtimeRecord : runtimeRecords) {
                analyseList.add(new DevMoldFaultDurationAnalyseVO(runtimeRecord.getMoldNo(), 2L, runtimeRecord.getStartTime().getTime(), runtimeRecord.getEndTime().getTime(), runtimeRecord.getDuration().longValue()));
            }
        }
        // 查询模具故障类型时长统计
        List<DevMoldFaultDurationCommonVO> moldFaultDurationList = moldRepairRecordDao.statMoldFaultDurationItem(param);
        if (CollectionUtil.isNotEmpty(moldFaultDurationList)) {
            for (DevMoldFaultDurationCommonVO faultDurationItem : moldFaultDurationList) {
                analyseList.add(new DevMoldFaultDurationAnalyseVO(faultDurationItem.getName(), 0L, faultDurationItem.getStartTime().getTime(), faultDurationItem.getEndTime().getTime(), faultDurationItem.getDuration()));
            }
        }
        // 获取设备故障类别时长统计
        List<DevMoldFaultDurationCommonVO> devFaultDurationList = devMaintenWorkDao.statDevFaultDurationItem(param);
        if (CollectionUtil.isNotEmpty(devFaultDurationList)) {
            for (DevMoldFaultDurationCommonVO faultDurationItem : devFaultDurationList) {
                analyseList.add(new DevMoldFaultDurationAnalyseVO(faultDurationItem.getName(), 1L, faultDurationItem.getStartTime().getTime(), faultDurationItem.getEndTime().getTime(), faultDurationItem.getDuration()));
            }
        }
        return analyseList;
    }

    /**
     * queryDevMoldFaultDurationDetail
     *
     * @param param 查询参数
     * @return
     */
    @Override
    public List<DevMoldFaultDurationDetailVO> queryDevMoldFaultDurationDetail(Map<String, Object> param) {
        List<DevMoldFaultDurationDetailVO> detailList = CollectionUtil.newArrayList();
        // 查询模具故障类型时长统计
        List<DevMoldFaultDurationDetailVO> faultTypeDurationStat = moldRepairRecordDao.getFaultTypeDurationStat(param);
        if (CollectionUtil.isNotEmpty(faultTypeDurationStat)) {
            detailList.addAll(faultTypeDurationStat);
        }
        // 获取设备故障类别时长统计
        List<DevMoldFaultDurationDetailVO> faultTypeDurationList = devMaintenWorkDao.getFaultTypeDurationStat(param);
        if (CollectionUtil.isNotEmpty(faultTypeDurationList)) {
            detailList.addAll(faultTypeDurationList);
        }
        return detailList;
    }

    /**
     * queryDevMoldFaultDurationDistribution
     *
     * @param param 查询参数
     * @return
     */
    @Override
    public DevMoldFaultDurationDistributionVO queryDevMoldFaultDurationDistribution(Map<String, Object> param) {
        DevMoldFaultDurationDistributionVO distributionStat = new DevMoldFaultDurationDistributionVO();
        List<DevMoldFaultDurationDistributionItemVO> inner = CollectionUtil.newArrayList();
        List<DevMoldFaultDurationDistributionItemVO> outer = CollectionUtil.newArrayList();
        String devId = MapUtil.getStr(param, "devId");
        String startDate = MapUtil.getStr(param, "startDate");
        String endDate = MapUtil.getStr(param, "endDate");

        // 获取设备运行时间
        List<DevMoldRuntimeRecordEntity> runtimeRecords = devMoldRuntimeRecordDao.criteriaQuery(devId, startDate, endDate);
        if (CollectionUtil.isNotEmpty(runtimeRecords)) {
            // 获取运行总时长
            Integer totalDuration = runtimeRecords.stream().map(DevMoldRuntimeRecordEntity::getDuration).reduce(0, (x, y) -> x + y);
            inner.add(new DevMoldFaultDurationDistributionItemVO("正常运行时间", totalDuration));
            outer.add(new DevMoldFaultDurationDistributionItemVO("正常运行时间", totalDuration));
        }
        // 查询模具故障类型时长统计
        List<DevMoldFaultDurationDetailVO> faultTypeDurationStat = moldRepairRecordDao.getFaultTypeDurationStat(param);
        if (CollectionUtil.isNotEmpty(faultTypeDurationStat)) {
            // 设置模具异常时间
            Integer totalDuration = faultTypeDurationStat.stream().map(DevMoldFaultDurationDetailVO::getDuration).reduce(0, (x, y) -> x + y);
            inner.add(new DevMoldFaultDurationDistributionItemVO("模具异常时间", totalDuration));
            // 设置外圈模具不同故障类型下的时长分布
            for (DevMoldFaultDurationDetailVO detail : faultTypeDurationStat) {
                outer.add(new DevMoldFaultDurationDistributionItemVO(detail.getFaultType(), detail.getDuration()));
            }
        }
        // 获取设备故障类别时长统计
        List<DevMoldFaultDurationDetailVO> faultTypeDurationList = devMaintenWorkDao.getFaultTypeDurationStat(param);
        if (CollectionUtil.isNotEmpty(faultTypeDurationList)) {
            // 设置内圈设备故障总时长
            Integer totalDuration = faultTypeDurationList.stream().map(DevMoldFaultDurationDetailVO::getDuration).reduce(0, (x, y) -> x + y);
            inner.add(new DevMoldFaultDurationDistributionItemVO("设备异常时间", totalDuration));
            // 设置外圈设备不同故障类型下的时长分布
            for (DevMoldFaultDurationDetailVO detail : faultTypeDurationList) {
                outer.add(new DevMoldFaultDurationDistributionItemVO(detail.getFaultType(), detail.getDuration()));
            }
        }
        distributionStat.setInner(inner);
        distributionStat.setOuter(outer);
        return distributionStat;
    }

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevMoldRuntimeRecordEntity queryObject(String id) {
        return devMoldRuntimeRecordDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevMoldRuntimeRecordEntity> queryList(Map<String, Object> map) {
        return devMoldRuntimeRecordDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devMoldRuntimeRecordDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param devMoldRuntimeRecord 实体
     * @return
     */
    @Override
    public int save(DevMoldRuntimeRecordEntity devMoldRuntimeRecord) {
        devMoldRuntimeRecord.setId(RandomUtil.randomString(32));
        return devMoldRuntimeRecordDao.save(devMoldRuntimeRecord);
    }

    /**
     * update
     *
     * @param devMoldRuntimeRecord 实体
     * @return
     */
    @Override
    public int update(DevMoldRuntimeRecordEntity devMoldRuntimeRecord) {
        return devMoldRuntimeRecordDao.update(devMoldRuntimeRecord);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devMoldRuntimeRecordDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devMoldRuntimeRecordDao.deleteBatch(ids);
    }
}
